Ext.define('Admin.view.sys.DdsMRsvController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.ddsMRsvController',
    init: function() {},

    //状态管理
    state: {
        // refs: this.getReferences(), //所有的references的集合
    },

    //河流搜索
    riverSearch: function() {
        var refs = this.getReferences();
        // var serchName = refs.serchName.getValue();
        var serchRcd = refs.serchRcd.getValue();
        var grid = refs.riverMRGrid;
        var params = { rcd: serchRcd, river: config.RIVER };
        grid.getStore().getProxy().extraParams = params;
        grid.getStore().reload();
    },

    //删除
    riverDelete: function(grid, rowIndex, colIndex) {
        var resnm = grid.getStore().getAt(rowIndex).get('resnm');
        var rcd = grid.getStore().getAt(rowIndex).get('rcd');
        var rescd = grid.getStore().getAt(rowIndex).get('rescd');
        Ext.Msg.confirm("警告", "确定要删除吗？", function(button) {
            if (button === 'yes') {
                Ext.Ajax.request({
                    url: config.URL + '/emergency/ddsMRsv/delete',
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 1) {
                            grid.store.load();
                            Ext.Msg.alert('系统提示', "删除成功");
                        } else {
                            Ext.Msg.alert("系统提示", result.msg);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('系统提示', '系统出错！');
                    },
                    params: { resnm: resnm, rcd: rcd, rescd: rescd, river: config.RIVER }
                });
            }
        })
    },

    //新增
    riverAdd: function() {
        var _that = this;
        var win = _that.lookupReference('expertAddWindow');
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '新增河段模型水库',
                width: 530,
                layout: 'fit',
                //				modal: false,
                autoScroll: true,
                closeAction: 'hide',
                reference: 'expertAddWindow',
                buttons: [{
                        text: '保存',
                        handler: function() {
                            var form = _that.lookupReference('expertAddForm').getForm();
                            var modal = this.up('window');
                            var expertGrid = _that.lookupReference('riverMRGrid');
                            var store = expertGrid.getStore();
                            var values = form.getValues();
                            values.river = config.RIVER;
                            Ext.Ajax.request({
                                url: config.URL + '/emergency/ddsMRsv/save',
                                method: "post",
                                success: function(response, opts) {
                                    var result = Ext.JSON.decode(response.responseText);
                                    Ext.MessageBox.hide();
                                    if (result.code === 1) {
                                        Ext.Msg.alert('系统提示', "保存成功");
                                        store.reload();
                                        modal.hide();
                                    } else {
                                        Ext.Msg.alert("系统提示", result.msg);
                                    }
                                },
                                failure: function() {
                                    Ext.Msg.alert('系统提示', '系统出错！');
                                },
                                params: values
                            });
                        }
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            modal.hide()
                        }
                    },
                ],
                items: {
                    xtype: 'riverMRForm',
                    border: false,
                    reference: 'expertAddForm'
                }
            })
            this.getView().add(win)
        }
        win.show();
        this.lookupReference('expertAddForm').getForm().reset();
    },

    //修改
    riverUpdate: function(grid, rowIndex, colIndex) {
        var rec = grid.getStore().getAt(rowIndex);
        var _that = this;
        var win = this.lookupReference('expertUpdataWindow');
        var saveUpdata = function() {
            var form = _that.lookupReference('expertUpdataForm').getForm();
            var modal = this.up('window');
            var emergencyIncidentGrid = _that.lookupReference('riverMRGrid');
            var store = emergencyIncidentGrid.getStore();
            var values = form.getValues();
            values.river = config.RIVER;
            Ext.Ajax.request({
                url: config.URL + '/emergency/ddsMRsv/update',
                method: "post",
                success: function(response, opts) {
                    Ext.MessageBox.hide();
                    var result = Ext.JSON.decode(response.responseText);
                    if (result.code === 1) {
                        Ext.Msg.alert('系统提示', "修改成功");
                        store.reload();
                        modal.hide();
                    } else {
                        Ext.Msg.alert("系统提示", result.msg);
                    }
                },
                failure: function() {
                    Ext.Msg.alert('系统提示', '系统出错！');
                },
                params: values
            });
        };
        if (!win) {
            win = Ext.create('Ext.window.Window', {
                title: '修改河段模型水库',
                width: 530,
                layout: 'fit',
                //				modal: false,
                autoScroll: true,
                closeAction: 'hide',
                reference: 'expertUpdataWindow',
                buttons: [{
                        text: '保存',
                        handler: saveUpdata
                    },
                    {
                        text: '取消',
                        handler: function() {
                            var modal = this.up('window');
                            _that.lookupReference('expertUpdataForm').getForm().reset();
                            modal.hide()
                        }
                    },

                ],
                items: {
                    xtype: 'riverMRForm',
                    border: false,
                    reference: 'expertUpdataForm'
                }
            });
            this.getView().add(win);
        }
        this.lookupReference('expertUpdataForm').getForm().reset();
        this.lookupReference('expertUpdataForm').getForm().setValues(rec.data)
        win.show();
    }





});